//
//  FamilyManageVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/28.
//  Copyright © 2020 tym. All rights reserved.
//

#import "RoomManagerSunVC.h"
#import "MyFamilyApplyCell.h"
#import "Sizes.h"
#import "LYEmptyViewHeader.h"
#import "EquipmentService.h"
#import "RelatedEquipmentVC.h"
#import "RoomEditVC.h"
#import "RoomEditVC.h"
#import "UIButton+ImageTitleSpacing.h"
#import "RoomUserManagerVC.h"
#import "RoomMangeOneCell.h"
#import "RoomMangeTwoCell.h"
#import "CreateNewHomeViewController.h"
#import "JoinHomeViewController.h"
#import "AddFamilyView.h"
#import "FamilyOptionView.h"
#import "FamilyDescVC.h"
#import "ChangeFamilyVC.h"
#import "UserTool.h"
#import "UserModel.h"
#import "FamilyService.h"
#import "FamilyNameVC.h"
#import "FamilyDetailVC.h"
#import "BlacklistVC.h"
#import "FamilymemberVC.h"
#import "Colors.h"
#import "WHToast.h"
#import "ShowAlertView.h"
#import "Fonts.h"
#import "FamilyRoomService.h"
#import "TYMButton.h"
#import "TYMBaseTableViewCell.h"
#import "FamilyManageTwoCell.h"
#import "FamilyManageOneCell.h"
#import "GroupShadowTableView.h"
#import "FamilyManageThreeCell.h"
@interface RoomManagerSunVC ()<GroupShadowTableViewDelegate, GroupShadowTableViewDataSource>

@property (nonatomic, strong) GroupShadowTableView *tableView;

@property(nonatomic,strong) NSMutableArray *equmentArray;
//@property(nonatomic,strong) NSMutableArray *usersArray;

@property(nonatomic,weak) IBOutlet UIButton *optionBtn;

@property(nonatomic,strong) FamilyRoomService  *roomService;

@property(nonatomic,strong) EquipmentService *equmentService;
@end

@implementation RoomManagerSunVC

-(instancetype)initWithRoomModel:(RoomModel*)roomModel{
    self = [super init];
    if (self) {
        self.roomModel = roomModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTable];
    
    //if ([UserTool getCurrentFamily].familyRole== 2) {
        
        self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"img_empt_equipment"
         titleStr:@"该房间暂无设备"
        detailStr:@""];
        
        
  //  }

    
    [self.optionBtn setImage:[UIImage imageNamed:@"icon_home5"] forState:UIControlStateNormal];
    [self.optionBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:3];
    if ([UserTool getCurrentFamily].familyRole == 2) {
        self.optionBtn.hidden = YES;
    }
    
//    [self getRoomUsers];
    [self getRoomEquments];
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavigateonHeight-33-SafeBottomMargin-74) style:UITableViewStyleGrouped];
    self.tableView.groupShadowDelegate = self;
    self.tableView.groupShadowDataSource = self;
    self.tableView.backgroundColor = viewBGColor;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.01)];
    self.tableView.sectionFooterHeight = 0;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    self.optionBtn.layer.cornerRadius = 5;
    self.optionBtn.backgroundColor = [UIColor whiteColor];
    self.optionBtn.layer.borderWidth = 1;
    self.optionBtn.layer.borderColor = rgba(151, 151, 151).CGColor;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
}

//获取房间设备
-(void)getRoomEquments{
    TYMWeak(self);
    [self.equmentService getRoomequipments:_roomModel.id completion:^(LMJBaseResponse *respon, NSArray<EquipmentModel *> * _Nonnull array) {
        if (respon.message) {
            [WHToast showMessage:respon.message duration:2 finishHandler:nil];
        }else{
            if (weakself.equmentArray.count) {
                [weakself.equmentArray removeAllObjects];
            }
            [weakself.equmentArray addObjectsFromArray:array];
            [weakself.tableView reloadData];
            if(!weakself.equmentArray.count){
                [weakself.tableView ly_showEmptyView];
            }else{
                [weakself.tableView ly_hideEmptyView];
            }
            //[weakself showEmptyView];
        }
    }];
}

-(void)showEmptyView{
    if (!self.equmentArray.count && [UserTool getCurrentFamily].familyRole == 2) {
        [self.tableView ly_showEmptyView];
    }
}

//获取房间用户
//-(void)getRoomUsers{
//    TYMWeak(self);
//    [self.roomService getFindfamilyroomusersFamilyId:[UserTool getCurrentFamily].familyId familyRoomId:_roomModel.id   completion:^(LMJBaseResponse * _Nonnull response, NSArray * _Nonnull roosArry) {
//        if (response.message) {
//            [WHToast showMessage:response.message duration:2 finishHandler:nil];
//        }else{
//            if(weakself.usersArray.count){
//                [weakself.usersArray removeAllObjects];
//            }
//            [weakself.usersArray addObjectsFromArray:roosArry];
//            [weakself.tableView reloadData];
//            [weakself showEmptyView];
//        }
//
//    }];
//}

//关联设备
-(IBAction)relatedEquipmentFun:(id)sender{
    
    RelatedEquipmentVC *releateVC = [[RelatedEquipmentVC alloc] initWithRoomModel:self.roomModel];
    [self.navigationController pushViewController:releateVC animated:YES];
}

-(FamilyRoomService*)roomService{
    if (_roomService == nil) {
        _roomService = [[FamilyRoomService alloc] init];
    }
    return _roomService;
}

-(EquipmentService*)equmentService{
    if (_equmentService == nil) {
        _equmentService = [[EquipmentService alloc] init];
    }
    return _equmentService;
}

-(NSMutableArray*)equmentArray{
    if (_equmentArray == nil) {
        _equmentArray = [[NSMutableArray alloc] init];
    }
    return _equmentArray;
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 1;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
     if (section == 0) {
         if (!_equmentArray.count
             &&[UserTool getCurrentFamily].familyRole == 1&&_roomModel.roomType !=1) {
             return 1;
         }
         return  _equmentArray.count;
    }
    return 0;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   if (indexPath.section == 0){
        if (indexPath.row == 0) {
            if (!self.equmentArray.count && [UserTool getCurrentFamily].familyRole == 1) {
                return 46;
            }
            return [RoomMangeOneCell cellForHeight]+46;
        }
        return [RoomMangeOneCell cellForHeight];
    }
    return 0;
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
  if(indexPath.section == 0){
        RoomMangeOneCell *cell = [tableView dequeueReusableCellWithIdentifier:[RoomMangeOneCell identifier]];
        if (!cell) {
            cell = [[RoomMangeOneCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[RoomMangeOneCell identifier]];
        }
      
      if (indexPath.row == 0) {
          [cell showSectionView];
      }else{
          [cell hiddenSectionView];
      }
      cell.roomEditBlock = ^{
          RoomEditVC *editVC = [[RoomEditVC alloc] initWithRoom:self.roomModel WithEquments:self.equmentArray];
          
          editVC.pubRoomID = self.pubRoomId;
          [self.navigationController pushViewController:editVC animated:YES];
      };
      if (self.equmentArray.count) {
          [cell setEquModel:self.equmentArray[indexPath.row]];
          cell.conView.hidden = NO;
      }else{
          cell.conView.hidden = YES;
      }
      
      if (self.roomModel.roomType == 1) {
          cell.sectionBtn.hidden = YES;
      }else{
          cell.sectionBtn.hidden = NO;
      }
      return cell;
    }
    return [[UITableViewCell alloc] initWithFrame:CGRectZero];
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

@end
